package com.liesp.hrm.control;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.liesp.hrm.bean.Product;
import com.liesp.hrm.dao.ICartProductDAO;
import com.liesp.hrm.dao.impl.CartProductDAOImpl;
import com.liesp.hrm.exception.DAOException;
import com.liesp.hrm.exception.ServiceException;
import com.liesp.hrm.service.IProductService;
import com.liesp.hrm.service.impl.ProductServiceImpl;

public class CustomerServlet extends HttpServlet {
	
	private IProductService productService;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		 	response.setContentType("text/html;charset=gb2312");
	        request.setCharacterEncoding("gb2312");
	        
	        HttpSession session=request.getSession(true);
	        
	        String action=request.getParameter("action").trim();	        
	        String [] selectedCount=request.getParameterValues("choose");
	        List<Product> vec=new ArrayList<Product>();
	        if(action.equals("select")){
	            String productName=request.getParameter("productName");
	            String searchType=request.getParameter("searchType");
	            if(productName==null|| productName.equals("")){
	                session.setAttribute("namenull","namenull");
	                try {
						vec=productService.selectProduct("select * from product");
					} catch (ServiceException e) {
						this.forward(request,response,"error.jsp");
					}
	            }else if(searchType.equals("jingque")){
	                try {
						vec=productService.selectProduct("select * from product where product_name like '"+productName+"'");
					} catch (ServiceException e) {
						this.forward(request,response,"error.jsp");
					}
	            } else if(searchType.equals("mohu")){
	                try {
						vec=productService.selectProduct("select * from product where product_name like '%"+productName+"%'");
					} catch (ServiceException e) {
						this.forward(request,response,"error.jsp");
					}
	            }
	            session.setAttribute("result",vec);
	            this.forward(request,response,"searchview.jsp");
	        }else if(action.equals("getall")){
	            try {
					vec=productService.selectProduct("select * from product");
				} catch (ServiceException e) {
					this.forward(request,response,"error.jsp");
				}
	            session.setAttribute("result",vec);
	            this.forward(request,response,"searchview.jsp");
	        }else if(action.equals("next")){
	            String pageno=request.getParameter("pageno");
	            this.forward(request,response,"searchview.jsp?pageno="+pageno);
	        }else if(action.equals("previous")){
	            String pageno=request.getParameter("pageno");
	            this.forward(request,response,"searchview.jsp?pageno="+pageno);
	        }else if(action.equals("addtocart")){
	            if(selectedCount!=null){
	            	ICartProductDAO cartbean=null;
	                List tempvec=(List)session.getAttribute("result");
	                if(session.getAttribute("cart")==null){
	                    cartbean=new CartProductDAOImpl();
	                }else{
	                    cartbean=(ICartProductDAO)session.getAttribute("cart");
	                }
	                for(int i=0;i<selectedCount.length;i++){
	                    try {
							cartbean.addToCart(tempvec,selectedCount[i]);
						} catch (DAOException e) {
							this.forward(request,response,"error.jsp");
						}
	                }
	                session.setAttribute("cart",cartbean);
	            }
	            this.forward(request,response,"cartview.jsp");
	        }else if(action.equals("delete")){
	            if(selectedCount!=null){
	            	ICartProductDAO cartbean=(ICartProductDAO)session.getAttribute("cart");
	                for(int i=0;i<selectedCount.length;i++){
	                    cartbean.deleteFromCart(selectedCount[i]);
	                }
	                session.setAttribute("cart",cartbean);
	            }
	            this.forward(request,response,"cartview.jsp");
	        }else if(action.equals("gotocheckout")){
	            this.forward(request,response,"checkout.jsp");
	        }else if(action.equals("checkout")){
	        	ICartProductDAO cartbean=(ICartProductDAO)session.getAttribute("cart");
	        	try {
					productService.checkOut(cartbean);
				} catch (ServiceException e) {
					this.forward(request,response,"error.jsp");
				}
	            session.setAttribute("cart",null);
	            session.setAttribute("check","ok");
	            this.forward(request,response,"checkout.jsp");
	        }
	    }
	    
	   
	    
	    public String getServletInfo() {
	        return "Short description";
	    }
	    
	    private void forward(HttpServletRequest request,HttpServletResponse response,String url) throws ServletException,IOException {
	        RequestDispatcher dispatcher = request.getRequestDispatcher(url);
	        dispatcher.forward(request, response);
	    }

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doGet(req, resp);
	}

	@Override
	public void init() throws ServletException {
		productService = new ProductServiceImpl();
	}

}
